%% experimenting with exported variables from case constructs
%% result: 
%%    a variable can only be used outside, if it's assigned in all branches


-module(unsafedemo).
-export([vartest1/0, vartest2/0, vartest3/0, vartest4/0]).

vartest1() ->
    A = {1, 2, 3},
    case A of
    {1, X, 3}->
        X;
    _ -> nomatch
    end.
vartest2() ->
    Z = 1,
%    case Z of 
%       1 ->
%            A = 2;   % a is unsafe error
%       _ -> yo
%    end,
    X = 2,
    A = {1, 2, 3},
    case A of
    L when size(L) =:= 3, element(1, L) =:= 1, element(3, L) =:= 3, element(2, L) =:= X ->
        X = element(2, L),
        X;
    _ -> nomatch
    end.
    
vartest3() ->
    X = fun(A) ->
            B = A
        end,
    X(2).
%    B.  % B is unbound error tehát itt nincs bekavarás

vartest4() ->
    Z = {x, 4},
    case Z of
    {x, A} ->
        A;
    {z, B} ->
        B
    end,
    case Z of
    X when element(2, X) =:= A ->  %%ez a sor okozza az unsafe-séget, süti
        najo
    end.